#!/bin/bash
# 用法: ./snapshot.sh <视频路径> [模式: single|s|tile|t] [数量] [tile行数] [tile列数]
# 默认模式: single
# single 模式下数量可选，默认 6 张，并额外打包成 zip 压缩包
# tile 模式下数量、行数、列数可选，默认 16 帧、4x4，截图均匀分布

VIDEO="$1"
MODE="$2"
COUNT="$3"
TILEROW="$4"
TILECOL="$5"
OUTDIR="."  # 输出当前目录

# ========== 工具检测与安装 ==========
check_and_install() {
  local pkg=$1
  local cmd=$2

  if ! command -v "$cmd" >/dev/null 2>&1; then
    echo "缺少依赖: $pkg ($cmd)，尝试安装..."
    if command -v apt-get >/dev/null 2>&1; then
      sudo apt-get update && sudo apt-get install -y "$pkg"
    elif command -v apt >/dev/null 2>&1; then
      sudo apt update && sudo apt install -y "$pkg"
    elif command -v yum >/dev/null 2>&1; then
      sudo yum install -y "$pkg"
    elif command -v dnf >/dev/null 2>&1; then
      sudo dnf install -y "$pkg"
    elif command -v pacman >/dev/null 2>&1; then
      sudo pacman -Sy --noconfirm "$pkg"
    else
      echo "无法自动安装 $pkg，请手动安装后再运行脚本。"
      exit 1
    fi
  fi
}

check_and_install "ffmpeg" "ffmpeg"
check_and_install "zip" "zip"

# ========== 参数处理 ==========
if [ -z "$VIDEO" ]; then
  echo "用法: $0 <视频路径> [模式: single|s|tile|t] [数量] [tile行数] [tile列数]"
  exit 1
fi

# 默认模式
if [ -z "$MODE" ]; then
  MODE="single"
fi

# 模式缩写映射
if [[ "$MODE" == "s" ]]; then MODE="single"; fi
if [[ "$MODE" == "t" ]]; then MODE="tile"; fi

# ========== 获取视频时长 ==========
DURATION=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$VIDEO")
DURATION=${DURATION%.*}

# ========== 处理 single 模式 ==========
if [ "$MODE" == "single" ]; then
  if [ -z "$COUNT" ]; then COUNT=6; fi
  SNAPLIST=()
  BASENAME=$(basename "$VIDEO")
  BASENAME="${BASENAME%.*}"

  for i in $(seq 1 $COUNT); do
    RAND=$(shuf -i 0-$((DURATION-1)) -n 1)
    OUT="$OUTDIR/${BASENAME}_snapshot_$i.jpg"
    ffmpeg -ss "$RAND" -i "$VIDEO" -frames:v 1 -q:v 2 "$OUT" -y -loglevel quiet
    SNAPLIST+=("$OUT")
    echo "生成独立截图: $OUT (时间点: ${RAND}s)"
  done

  ZIPFILE="$OUTDIR/${BASENAME}_snapshots.zip"
  zip -j "$ZIPFILE" "${SNAPLIST[@]}" >/dev/null
  echo "已打包所有截图: $ZIPFILE"

# ========== 处理 tile 模式 ==========
elif [ "$MODE" == "tile" ]; then
  if [ -z "$COUNT" ]; then COUNT=16; fi
  if [ -z "$TILEROW" ]; then TILEROW=4; fi
  if [ -z "$TILECOL" ]; then TILECOL=4; fi

  TMPFILES=()
  BASENAME=$(basename "$VIDEO")
  BASENAME="${BASENAME%.*}"

  INTERVAL=$((DURATION / COUNT))
  for i in $(seq 0 $((COUNT-1))); do
    START=$((i * INTERVAL))
    END=$(((i+1) * INTERVAL - 1))
    if [ $END -lt $START ]; then END=$START; fi
    RAND=$(shuf -i $START-$END -n 1)
    TMP="$OUTDIR/tmp_$i.jpg"
    TMPFILES+=("$TMP")
    ffmpeg -ss "$RAND" -i "$VIDEO" -frames:v 1 -q:v 2 "$TMP" -y -loglevel quiet
    echo "生成临时截图: $TMP (时间点: ${RAND}s)"
  done

  PREVIEW="$OUTDIR/${BASENAME}_preview.jpg"
  ffmpeg -pattern_type glob -i "$OUTDIR/tmp_*.jpg" -filter_complex "tile=${TILECOL}x${TILEROW}" -frames:v 1 "$PREVIEW" -y -loglevel info

  if [ -f "$PREVIEW" ]; then
    rm -f "${TMPFILES[@]}"
    echo "生成 ${TILEROW}x${TILECOL} 缩略图: $PREVIEW"
  else
    echo "拼图生成失败，请检查 FFmpeg 日志"
  fi

else
  echo "模式错误，只能是 single|s 或 tile|t"
  exit 1
fi
